<template>
  <div id="app">
    <el-card shadow="always">


      <el-form :inline="true" size="medium" :model="tbspecSearch" class="demo-form-inline">

        <el-form-item >
          <el-input v-model="tbspecSearch.specName" placeholder="规格名称"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" plain @click="search" icon="el-icon-user">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" plain @click="edittbspec" icon="el-icon-plus">添加规格</el-button>
        </el-form-item>
      </el-form>

      <el-table
          :data="tbspecList"
          style="width: 100%">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            label="规格ID"
            prop="id"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            label="规格名称"
            align="center"
            prop="specName">
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="success"
                @click="handleEdit(scope.$index, scope.row)">修改</el-button>

          </template>
        </el-table-column>
      </el-table>

      <!--分页导航-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
    <!--对话框-->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      {{tbspec}}
      <el-form :model="tbspec">
        <el-form-item label="规格ID" :label-width="formLabelWidth">
          <el-input v-model="tbspec.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="规格名称" :label-width="formLabelWidth">
          <el-input v-model="tbspec.specName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="规格选项ID" :label-width="formLabelWidth">
          <el-input v-model="tbspec.specId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="规格选项名称" :label-width="formLabelWidth">
          <el-input v-model="tbspec.optionName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="规格选项订单" :label-width="formLabelWidth">
          <el-input v-model="tbspec.orders" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="savetbspec">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import tbspecApi from "@/api/TbSpecification"

export default {
  data() {
    return {
      tbspecList: [],
      page:1,
      pageSize:5,
      total:100,

      tbspecSearch: {},

      dialogFormVisible:false,
      formLabelWidth:'100px',
      tbspec: {},
      title: '添加规格',
    }
  },
  created() {



    this.search()
  },

  methods: {

    findtbspecs(){
      this.$axios.get('/tbspecification/list.do')
          .then(res => {

            console.log("数据：",res.data)
            this.tbspecList = res.data
          })


    },

    async findByPage(){

      const {data:res} = await tbspecApi.findByPage(this.page,this.pageSize)
      this.tbspecList = res.rows
      this.total = res.total
    },



    async search(){
      const {data} = await tbspecApi.search(this.page,this.pageSize,this.tbspecSearch)
      this.tbspecList = data.rows
      this.total = data.total
    },

    edittbspec(){
      this.dialogFormVisible = true
    },

    async savetbspec(){
      const {data:res} = await tbspecApi.save(this.tbspec)
      if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })

        this.dialogFormVisible = false

        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },

    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    // 每页大小改变时事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;

      this.search()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;

      this.search()
    }
  }
}
</script>
<style>
.el-card{
  width: 90%;
//margin: 0 auto;
}
#app{
  display: flex;
  justify-content: center;
}
.el-pagination{
  text-align: right;
  margin-top: 15px;
}
</style>
